package com.anjie.powerproject.controller;

import com.anjie.powerproject.entity.GraphInfo;
import com.anjie.powerproject.entity.ItemInfo;
import com.anjie.powerproject.entity.Manager;
import com.anjie.powerproject.entity.StationInfo;
import com.anjie.powerproject.service.GraphInfoService;
import com.anjie.powerproject.service.ManagerService;
import com.anjie.powerproject.service.StationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manager")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private StationInfoService stationInfoService;

    @Autowired
    private GraphInfoService graphInfoService;
    /**
     * 验证管理员账号是否存在，若失败给出提示，若成功返回指定管理的页面
     * @return
     */
    @PostMapping("/login")
    public String managerLogin(HttpServletRequest request, Manager manager, Model model, HttpSession session){
        Manager manager1 = managerService.loginIssucceed(manager.getManagerName(),manager.getManagerPwd());
        if(manager1 == null){
            model.addAttribute("message","账号输入错误或不存在");
            return "login";
        }
        session.setAttribute("loginManager",manager1.getManagerName());
        model.addAttribute("remark",manager1.getRemark());
        List<GraphInfo> allGraphInfo = graphInfoService.getAllGraphInfo();
        model.addAttribute("graphInfoList",allGraphInfo);

        List<StationInfo> stationInfoList = stationInfoService.findAllInfo();

        StationInfo stationInfo = stationInfoList.get(0);
        String stationName = stationInfo.getStationName();
        System.out.println(stationName);
        model.addAttribute("stationInfoList_show_name",stationName);

        //登录后返回的page
//        return "showPage1.html";
        return "main";
    }

    @RequestMapping("/loginOut1")
    public String managerLoginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("loginManager");
        return "login";
    }

    /**
     * 注册管理员账号，用户名不能重复,标识符不能为空,校验在前端完成(测试)
     * @param manager
     * @return
     */
    @PostMapping("/regist")
    @ResponseBody
    public Map managerRegist(Manager manager){
        Map map = new HashMap();
        if(manager.getRemark().isEmpty()){
            map.put("message","标识符不能为空");
        }else{
            ItemInfo itemInfo = managerService.registRemarkIsExist(manager.getRemark());
            Manager manager1 = managerService.registNameIsExist(manager.getManagerName());
            if(manager1 == null && itemInfo != null){
                managerService.registManager(manager);
                map.put("message","注册成功");
            }else {
                map.put("message","当前用户名已存在 或 管理楼栋不存在为无效注册");
            }
        }
        return map;
    }

    /**
     * 根据id删除用户(测试)
     * @param managerId
     * @return
     */
    @GetMapping("/delete/{managerId}")
    @ResponseBody
    public Map managerDelete(@PathVariable("managerId") Integer managerId){
        Map map = new HashMap();
        boolean flag = managerService.deleteManagerById(managerId);
        System.out.println(flag);
        if(flag)
            map.put("message","成功删除");
        else
            map.put("message","删除用户不存在 或 删除失败");
        return map;
    }

    /**
     * 获取所有用户信息(测试)
     * @return
     */
    @GetMapping("/getAll")
    @ResponseBody
    public List<Manager> managerGetAll(){
        return managerService.getAllManager();
    }
}
